package net.wanho.service.impl;

import net.wanho.po.OrderGoods;
import net.wanho.mapper.OrderGoodsMapper;
import net.wanho.service.OrderGoodsService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 商品-订单关联表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class OrderGoodsServiceImpl implements OrderGoodsService {

    private final OrderGoodsMapper orderGoodsMapper;

    /**
     * 商品-订单关联表分页查询
     * @param pageNum
     * @param pageSize
     * @param orderGoods
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, OrderGoods orderGoods) {
        if(ObjectUtil.isNull(orderGoods)){
            orderGoods = new OrderGoods();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<OrderGoods> orderGoodsList = orderGoodsMapper.select(orderGoods);
        return new PageInfo(orderGoodsList);
    }

    /**
     * 根据主键查询商品-订单关联表
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public OrderGoods findById(Serializable id) {
        return orderGoodsMapper.selectById(id);
    }

    /**
     * 商品-订单关联表新增
     * @param orderGoods
     */
    @Override
    public void add(OrderGoods orderGoods) {
        orderGoodsMapper.insert(orderGoods);
    }

    /**
     * 修改商品-订单关联表
     * @param orderGoods
     */
    @Override
    public void modify(OrderGoods orderGoods) {
        orderGoodsMapper.update(orderGoods);
    }

    /**
     * 根据主键删除商品-订单关联表
     * @param id
     */
    @Override
    public void removeById(Serializable id) {
        if(((String)id).contains(",")){
            orderGoodsMapper.deleteByIds(((String)id).split(","));
        }else {
            orderGoodsMapper.deleteById(id);
        }
    }
}
